{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import docx\n",
    "import re\n",
    "from docx import Document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "docx_name=[\"教学内容分析.docx\", \"学情分析.docx\", \"教学目标.docx\", \"教学重难点.docx\", \"教学准备.docx\", \"教学方法.docx\",\"教学过程设计.docx\", \"教学环节.docx\", \"教学活动.docx\", \"学习评价.docx\", \"作业设计.docx\", \"板书设计.docx\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "23"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "category_name = [\n",
    "    \"教学内容分析\",\"教材分析\",\"内容分析\",\"教学内容\",\n",
    "    \"学情分析\",\"学习者分析\",\n",
    "    \"教学目标\",\"学习目标\",\n",
    "    \"教学重难点\",\"教学重点\",\"教学难点\",\n",
    "    \"教学准备\",\n",
    "    \"教学方法\",\n",
    "    \"教学过程\",\n",
    "    \"教学环节\",\n",
    "    \"教学活动\",\n",
    "    \"学习评价\",\"学习效果评价设计\",\n",
    "    \"作业设计\",\"布置作业\", \"课时作业\",\"课后作业\",\n",
    "    \"板书设计\"\n",
    "]\n",
    "len(category_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def judge_category(text):\n",
    "    result = -1\n",
    "    for i in range(len(category_name)):\n",
    "        if re.search(category_name[i],text) != None:\n",
    "            if i in (0,1,2,3):\n",
    "                return 0\n",
    "            elif i in (4,5):\n",
    "                return 1\n",
    "            elif i in (6,7):\n",
    "                return 2\n",
    "            elif i in (8,9,10):\n",
    "                return 3\n",
    "            elif i == 11:\n",
    "                return 4\n",
    "            elif i == 12:\n",
    "                return 5\n",
    "            elif i == 13:\n",
    "                return 6\n",
    "            elif i == 14:\n",
    "                return 7\n",
    "            elif i == 15:\n",
    "                return 8\n",
    "            elif i in (16,17):\n",
    "                return 9\n",
    "            elif i in (18,19,20,21):\n",
    "                return 10\n",
    "            elif i == 22:\n",
    "                return 11\n",
    "        else:\n",
    "            result = -1\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "judge_category('教学重点：\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def txt_save_docx(docx_path):\n",
    "    with open(docx_path, 'r', encoding='utf-8') as file:\n",
    "        doc = Document()\n",
    "        is_firstparagraph = 1\n",
    "        for line in file:\n",
    "            category = judge_category(line)\n",
    "            if is_firstparagraph == 1 and category == -1:\n",
    "                continue\n",
    "            else:\n",
    "                if category != -1 :\n",
    "                    if is_firstparagraph == 1:\n",
    "                        save_category = category\n",
    "                    else:\n",
    "                        doc.save(docx_name[save_category])\n",
    "                        save_category = category\n",
    "                        doc = Document()\n",
    "                else:\n",
    "                    doc.add_paragraph(line)\n",
    "                is_firstparagraph = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def docx_save_docx(docx_path):\n",
    "    docin = Document(docx_path)\n",
    "    doc = Document()\n",
    "    is_firstparagraph = 1\n",
    "    for line in docin.paragraphs:\n",
    "        category = judge_category(line.text)\n",
    "        if is_firstparagraph == 1 and category == -1:\n",
    "             continue\n",
    "        else:\n",
    "            if category != -1 :\n",
    "                if is_firstparagraph == 1:\n",
    "                    save_category = category\n",
    "                else:\n",
    "                    doc.save(docx_name[save_category])\n",
    "                    save_category = category\n",
    "                    doc = Document()\n",
    "            else:\n",
    "                doc.add_paragraph(line.text)\n",
    "            is_firstparagraph = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt_save_docx(\"output.txt\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
